package com.huoyun.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import com.huoyun.base.bean.NodeInfo;
import com.huoyun.base.service.NodeInfoService;

/**
 * 下拉框选项的键值关系
 * 
 * @author Administrator
 * 
 */
public final class CodeBook {

	private static final CodeBook INSTANCE = new CodeBook();

	private Map<String, Map<Integer, String>> codeBook = null;

	private CodeBook() {
		codeBook = new HashMap<String, Map<Integer, String>>();
		init();
	}

	public static final CodeBook getInstance() {
		return INSTANCE;
	}

	/**
	 * 初始化codebook
	 */
	private void init() {
		final WebApplicationContext wac = ContextLoader
				.getCurrentWebApplicationContext();
		final NodeInfoService nodeInfoService = (NodeInfoService) wac
				.getBean("nodeInfoService");
		final List<NodeInfo> allNode = nodeInfoService.getAllNodeInfos();

		for (final NodeInfo node : allNode) {
			Map<Integer, String> modeGroup = codeBook.get(node.getModeName());
			if (modeGroup == null) {
				modeGroup = new HashMap<Integer, String>();
				codeBook.put(node.getModeName(), modeGroup);
			}
			modeGroup.put(node.getSequence(), node.getName());
		}
	}

	/**
	 * 取得货物类型键值对
	 * 
	 * @return 货物类型
	 */
	public Map<Integer, String> getCargosType() {
		Map<Integer, String> cargoType = codeBook.get("cargosType");
		if (cargoType == null) {
			cargoType = new HashMap<Integer, String>();
		}
		return cargoType;
	}
}
